<?php

namespace Shared\LocalSyntaxSpecific\LRCModifier;

use Shared\LocalSyntaxSpecific\ModifierInterface;

/**
 * 
 * @author Jordan Minchev <j.minchev@object.bg>
 * @copyright (c) 2014, ASPECT.BG Ltd.
 */
abstract class ContentModifier implements ModifierInterface {

    private $quoteMap = array(
        '”',
        '“',
        '„',
        '"',
        '&quot;',
        '&bdquo;',
        '&ldquo;',
        '&rdquo;',
        '&#147;', //&ldquo; 
        '&#148;', //&rdquo;
        '&#34;', //&quot;
        '&#8222;', //&bdquo;
        '&#8223;' //double high-reversed-9 quotation mark
    );
    private $defaultQuoteCharacter = '&quot;';
    protected $leftQuoteCharacter = '"';
    protected $rightQuoteCharacter = '"';
    protected $quoteCharacterPrefix = '<i class="quote">';
    protected $quoteCharacterSuffix = '</i>';

    /**
     * @param string $text
     * @param \Shared\Main\Language $Langauge
     * @return string
     */
    public function fixQuotes($text, $isHTML = false) {
        $text = str_replace($this->quoteMap, $this->defaultQuoteCharacter, $text);

        $rightReplacement = $this->rightQuoteCharacter;
        $leftReplacement = $this->leftQuoteCharacter;

        if ($isHTML) {
            $rightReplacement = $this->quoteCharacterPrefix . $rightReplacement . $this->quoteCharacterSuffix;
            $leftReplacement = $this->quoteCharacterPrefix . $leftReplacement . $this->quoteCharacterSuffix;
        }

        $text = preg_replace('#([^\s\(\[])' . $this->defaultQuoteCharacter . '#', '$1' . $rightReplacement, $text);
        $text = str_replace($this->defaultQuoteCharacter, $leftReplacement, $text);

        return $text;
    }

    public function fixPlainText($text, $isHTML = false) {
//        $string = $this->fixQuotes($text, $isHTML);
//        if ($isHTML) {
//            $string = $this->fixDate($string);
//        }
        return $text;
    }

    public function fixHTML(\Shared\LocalSyntaxSpecific\HTML\Parser $HTMLParser, $isHTML = false) {
        $text = $HTMLParser->getContent();

//        $text = $this->clearQuotes($text);

//        $text = $this->fixQuotes($text, $isHTML);
//        $text = $this->fixDate($text);

        $HTMLParser->setContent($text);

        return $HTMLParser;
    }

    public function fixDate($string) {
        return preg_replace('/([0-9]{4})\s+(г\.)/iu', '$1&nbsp;$2', $string);
    }

    public function clear($string) {
        return $this->clearDate($this->clearQuotes($string));
    }

    public function clearDate($string) {
        return preg_replace('/([0-9]{4})&nbsp;(г\.)/iu', '$1 $2', $string);
    }

    public function clearQuotes($string) {
        $count = 0;
        do {
            $string = preg_replace('#\<em class="quote"\>(.*?)\<\/em\>#', '$1', $string, $limit = -1, $count);
        } while ($count > 0);

        $count = 0;
        do {
            $string = preg_replace('#\<span class="quote"\>(.*?)\<\/span\>#', '$1', $string, $limit = -1, $count);
        } while ($count > 0);
        
        $pattern = $this->quoteCharacterPrefix . '(.*?)' . $this->quoteCharacterSuffix;
        $pattern = str_replace('/', '\/', $pattern);
        $pattern = str_replace('<', '\<', $pattern);
        $pattern = str_replace('>', '\>', $pattern);

        $count = 0;
        do {
            $string = preg_replace('#' . $pattern . '#', '$1', $string, $limit = -1, $count);
        } while ($count > 0);
        return $string;
    }

}
